package com.huarun.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.huarun.common.util.JedisUtils;

@Service("redisService")
public class RedisServiceImpl implements RedisService{
	
	private static Logger logger = LoggerFactory.getLogger(RedisServiceImpl.class);
	
	@Override
	public boolean saveObject(RedisKeyPrefix keyPrefix, String key, String value, int cacheTime) {
		String cacheKey = getCacheKey(keyPrefix, key);
		String result = JedisUtils.set(cacheKey, value, cacheTime);
		logger.info("saveObject result {} ", result);
		return "OK".equals(result);
	}
	
	@Override
	public boolean saveObject(RedisKeyPrefix keyPrefix, String key, Object obj, int cacheTime) {
		String cacheKey = getCacheKey(keyPrefix, key);
		String result = JedisUtils.setObject(cacheKey, obj, cacheTime);
		logger.info("saveObject result {} ", result);
		return "OK".equals(result);
	}

	@Override
	public String getValue(RedisKeyPrefix keyPrefix, String key) { 
		String cacheKey = getCacheKey(keyPrefix, key);
		String result = JedisUtils.get(cacheKey);
		logger.info("getValue result {} ", result);
		return result;
	}
	
	@Override
	public String getValueAddCacheTime(RedisKeyPrefix keyPrefix, String key,int expireTime) { 
		String cacheKey = getCacheKey(keyPrefix, key);
		String result = JedisUtils.get(cacheKey);
		JedisUtils.setExpire(cacheKey, expireTime);
		logger.info("getValue result {} and update expireTime ", result);
		return result;
	}

	@Override
	public long deleteValue(RedisKeyPrefix keyPrefix, String key) {
		String cacheKey = getCacheKey(keyPrefix, key);
		long index = JedisUtils.del(cacheKey);
		logger.info("del {} not exists", index);
		return index;
	}

	@Override
	public long deleteObj(RedisKeyPrefix keyPrefix, String key) {
		String cacheKey = getCacheKey(keyPrefix, key);
		long index = JedisUtils.delObject(cacheKey);
		logger.info("del {} not exists", index);
		return index;
	}
}
